package com.zhoug.common3.filters;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import com.zhoug.common3.utils.LogUtils;

import java.util.regex.Pattern;

/**
 * 限制输入的最大值
 * 注最小值不好限制,比如限制最小值为5,当想输入23的时候,输入2会被拦截到
 *
 * @Author: zhoug
 * @Date: 2023/1/4
 * @Description:
 */
public class MaxInputFilter implements InputFilter {
    private static final String TAG = ">>>MaxInputFilter";
    private static boolean DEBUG = true;

    private double mMax = Double.MIN_VALUE;//最大值
    /**
     * 是否包含最大值 默认true
     */
    private boolean mInclusiveMax = true;


    private static Pattern numberPatter = Pattern.compile("^[0-9|.]*$");

    public MaxInputFilter(double mMax) {
        this(mMax, true);
    }

    /**
     * @param mMax         最大值
     * @param inclusiveMax 是否包含最大值
     */
    public MaxInputFilter(double mMax, boolean inclusiveMax) {
        this.mInclusiveMax = inclusiveMax;
        this.mMax = mMax;
    }

    /**
     * @param max 最大值
     * @return
     */
    public MaxInputFilter setMax(double max) {
        this.mMax = max;
        return this;
    }

    /**
     * @param inclusiveMax 是否包含最大值 默认true
     * @return
     */
    public MaxInputFilter setInclusiveMax(boolean inclusiveMax) {
        this.mInclusiveMax = inclusiveMax;
        return this;
    }


    /**
     * 当缓冲区要使用source的[start - end)范围内的内容替换dest的[dstart - dend)范围内的内容
     *
     * @param source 输入的内容,若是删除动作，则为空字符
     * @param start  source对应的起始索引，始终为0
     * @param end    source对应的结尾索引，为source的length-1
     * @param dest   当前输入框中显示的内容
     * @param dstart 被贴换的内容在输入框中的起始位置(包含)
     * @param dend   被贴换的内容在输入框中的结束位置(不包含)
     * @return 返回null:表示接受原本的输入source;返回空字符串"":放弃本次输入,删除操作不会过滤掉;返回指定内容
     */
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (DEBUG) {
            LogUtils.d(TAG, "source:" + source + ",start:" + start + ",end:" + end + ",dest:" + dest + ",dstart:" + dstart + ",dend:" + dend);
        }
        String sourceText = source.toString();
        //去掉空格
        sourceText = sourceText.replaceAll(" ", "");
        //只能包含数字
        if (sourceText.length() != 0 && !numberPatter.matcher(sourceText).matches()) {
            if (DEBUG) {
                LogUtils.d(TAG, "输入内容不是数字");
            }
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder(dest);
        stringBuilder.replace(dstart, dend, sourceText);
        String result = stringBuilder.toString();
        if (DEBUG) {
            LogUtils.d(TAG, "result:" + result);
        }

        if (TextUtils.isEmpty(result)) {
            return "";
        }
        double re = 0;
        try {
            re = Double.parseDouble(result);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (DEBUG) {
            LogUtils.d(TAG, "re:" + re);
        }
        boolean match = true;
        if (re != Double.MIN_VALUE) {
            if (mInclusiveMax) {
                match = re <= mMax;
            } else {
                match = re < mMax;
            }
        }
        if (!match) {
            if (DEBUG) {
                LogUtils.d(TAG, "输入后不匹配");
            }
            //通过添加已删除的内容达到不删除的效果
            if (start == 0 && end == 0 && dend > dstart) {
                return dest.subSequence(dstart, dend);
            }
            return "";
        }
        return sourceText;
    }
}
